﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Qibla.Library;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private readonly Color DEFAULT_BACK_COLOR = Color.FromArgb(253, 255, 253);
        private readonly Color DEFAULT_MAKKAH_LINE_COLOR = Color.Lime;
        private readonly Color DEFAULT_NORTH_LINE_COLOR = Color.Red;
        private readonly Color DEFAULT_DIRECTION_COLOR = Color.Blue;
        private readonly Color DEFAULT_GRID_COLOR = Color.WhiteSmoke;
        private const int EDGE_BUFFER = 20;

        private readonly PointD NORTH = new PointD(0, 90);
        private readonly PointD MAKKAH =
            new PointD(39.8261797428131, 21.422469806126873);

        private Color _backColor;
        private Color _makkahLineColor;
        private Color _northLineColor;
        private Color _directionColor;
        private Color _gridColor;

        private double _distanceTravelled;
        private double _scalePixelsPerMeter;
        bool _showArrows = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            compass(
                new PointD(79.1, 6.1),
                pictureBox1.Width,
                pictureBox1.Height,
                100);
        }

        private void compass(PointD currentPosition, double screenWidth, double screenHeight, double heading)
        {
            PointD north = new PointD(currentPosition.Longitude, 90);
            PointD makkah = MAKKAH;

            // Find the center of the screen
            Point screenCenter = new Point();
            screenCenter.X = (int) Math.Truncate(screenWidth / 2);
            screenCenter.Y = (int) Math.Truncate(screenHeight / 2);

            // if center x = currentPos.X
            // if center y = currentPos.Y
            // what is makkah x and y
            // I.e. find the offset coordinates of Makkah from the center (i.e. current pos)
            PointD offsetMakkah = GetOffsetCoordinateFromPoint(makkah, currentPosition);
            PointD offsetNorth = GetOffsetCoordinateFromPoint(north, currentPosition);

            Point screenMakkah = GetScreenCoordinate(screenCenter, offsetMakkah);
            Point screenNorth = GetScreenCoordinate(screenCenter, offsetNorth);
            
        }

        private Point GetScreenCoordinate(Point screenCenter, PointD sourcePoint)
        {
            // Screen
            // -->0+ x
            // |
            // |
            // v
            // +
            // y
            
            Point pointToPlot = new Point();
            pointToPlot.Y = (int)(Math.Truncate(screenCenter.Y - sourcePoint.Y));
            pointToPlot.X = (int)(Math.Truncate(sourcePoint.X + screenCenter.X)) ;

            return pointToPlot;

        }

        /// <summary>
        /// Returns an offset point around another point
        /// </summary>
        /// <param name="pointToOffset"></param>
        /// <param name="offsetSource"></param>
        /// <returns></returns>
        private PointD GetOffsetCoordinateFromPoint(PointD pointToOffset, PointD offsetSource)
        {
            PointD offsetPoint = new PointD(
              pointToOffset.Longitude - offsetSource.Longitude,
              pointToOffset.Lattitude - offsetSource.Lattitude);

            return offsetPoint;
        }
    }
}
